import React, { useState, useEffect, useReducer } from 'react';

// 处理函数
const reduce = (oldState, action) => {
    let newState = JSON.parse(JSON.stringify(oldState))
    switch(action.type) {
        case 'minus':
            newState.count-- // 不对原来的数据经常操作
            return newState // 修改完以后在 return 出去
        case 'add':
            newState.count++
            return newState
        default:
            return oldState
    }
}

// 外部的对象
const intialState = {
    count: 0
}

export default function(props){  
                                    //  传处理函数 和  初始状态
    const [state, dispatch] = useReducer(reduce, intialState)

  return (
    <div>
        <button onClick={() => {
            dispatch({
                type: 'minus'
            })
        }} > - </button>
        {state.count}
        <button  onClick={() => {
            dispatch({
                type: 'add'
            })
        }} > + </button>
    </div>
  )
}